/**
 *******************************************************************************
 * @file simpleFilter.h
 * @author .ISS_AUTO (hudandan@issauto.com)
 * @date 2024-09-14  1. 平均滤波
 *                   2. 递推平均滤波
 *                   3. 限幅滤波
 *                   4. 一阶滤波
 *       2025-03-30  1. 优化
 *
 * @brief This file is for some interface of the simple digital filters.
 * @copyright Copyright (c) 2024 ISSAUTO TECH Co., Ltd. All rights reserved.
 *
 *******************************************************************************
 */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __SIMPLE_FILTER_H
#define __SIMPLE_FILTER_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
#define FILTER_TYPEDEF_FLOAT     (0U)

#if (FILTER_TYPEDEF_FLOAT)
typedef float filterSum_t;
typedef float filter_t;

#else
typedef unsigned int filterSum_t;
typedef unsigned int filter_t;

#endif

/* Exported types ------------------------------------------------------------*/

typedef struct {
    filterSum_t Sum;
    filterSum_t Max; //!< maximum
    filterSum_t Min; //!< minimum
    filter_t    *datBuf;

    unsigned short bufSize;
    unsigned short bufCnt;
    unsigned char  shiftNum : 7;
    unsigned char  tag      : 1;

} AveFilter_Typedef;

typedef struct {
    unsigned short limitAmp;
    unsigned short limitAmpCntUp;

    unsigned short Up;
    unsigned short Down;

} LimitAmpCnt_Typedef;

typedef struct {
    float    FactorA;
    unsigned short Up;
    unsigned short Down;

} FO_LimitAmpCnt_Typedef;

/* Exported constants --------------------------------------------------------*/
/* Comparative Result */
#define COMPARE_RESULT_INVALID         (-1)
#define COMPARE_RESULT_NONE            (0)
#define COMPARE_RESULT_OVER            (1)
#define COMPARE_RESULT_UNDER           (2)

/* Exported macro ------------------------------------------------------------*/
enum {
    FILTER_CHANNEL_0 = 0,
    FILTER_CHANNEL_1,

    FILTER_CHANNEL_2,
    FILTER_CHANNEL_3,
    FILTER_CHANNEL_4,
    FILTER_CHANNEL_5,
    FILTER_CHANNEL_6,
    FILTER_CHANNEL_7,
    FILTER_CHANNEL_8,
    FILTER_CHANNEL_9,
    FILTER_CHANNEL_10,

    FILTER_MAX_NUM
};

/* Exported variables --------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
AveFilter_Typedef Average_Filter_Register(filter_t *datBuf, unsigned char shiftNum, unsigned char bufSize);
void Average_Filter_Clear(AveFilter_Typedef *p_filter);
filter_t Average_Filter(AveFilter_Typedef *p_filter, filter_t new, filter_t old);
filter_t Move_Average_Filter(AveFilter_Typedef *p_filter, filter_t new);
filter_t Move_Average_MM_Filter(AveFilter_Typedef *p_filter, filter_t new);

LimitAmpCnt_Typedef Amplitude_Limiter_Filter_Register(unsigned short limitAmp, unsigned short limitAmpCntUp);
int Amplitude_Limiter_Filter(LimitAmpCnt_Typedef *counter, filter_t old, filter_t new);
int Amplitude_Limiter_Compare(unsigned short *tempCnt, float curVal, float threshold, float range, unsigned short cntNum);

float FirstOrder_Digit_Filter(float lastVal, float inVal, float Lv_Bo);
filter_t FirstOrder_Amplitude_Limiter_Filter(FO_LimitAmpCnt_Typedef *counter, filter_t lastVal, filter_t inVal);


#ifdef __cplusplus
}
#endif

#endif/* __SIMPLE_FILTER_H */

/* END OF FILE ---------------------------------------------------------------*/
